home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / plotting / contour / contour.lha / Contour / array.c next >
Encoding:
C/C++ Source or Header  |  1990-08-28  |  1.2 KB  |  60 lines

  1. /*  array.c - subroutines to dynamically allocate space for a 2D array */
  2.  
  3. #include <stdio.h>
  4. #include "contour.h"
  5.  
  6. /* 
  7.  * assign array[i][j] 
  8.  * i_size, j_size are the maximum dimensions of the 2D array.
  9.  */
  10. data *create_data_array(isize,jsize)
  11. int isize, jsize;
  12. {
  13.    char     *malloc();
  14.    data     *newptr;
  15.    unsigned int arr_size;
  16.  
  17.    /* figure out total no of elements */
  18.    arr_size = (unsigned)(isize*jsize)*sizeof(data);
  19.  
  20.    /* allocate space using malloc(), and return a pointer */
  21.    if ( (newptr = (data *)malloc(arr_size)) == NULL) {
  22.      fprintf(stderr,"Out of memory\n");
  23.      exit(1);
  24.    }
  25.  
  26.    /* return the pointer */
  27.    return(newptr);
  28. }
  29.  
  30. assign_data_array_value(arrptr,i,j,isize,jsize,val)
  31. data *arrptr,*val;
  32. int i,j,isize,jsize;
  33. {
  34.    if (i>=isize || j>=jsize) {
  35.       fprintf(stderr,"Element [%d][%d] is out of bounds!\n",i,j);
  36.       exit(1);
  37.    }
  38.    *(arrptr + i*jsize + j) = *val;
  39. }
  40.  
  41. data get_data_array_value(arrptr,i,j,isize,jsize)
  42. data *arrptr;
  43. int i,j,isize,jsize;
  44. {
  45.    data val;
  46.    if (i>=isize || j>=jsize) {
  47.       fprintf(stderr,"Element [%d][%d] is out of bounds!\n",i,j);
  48.       exit(1);
  49.    }
  50.    val = *(arrptr + i*jsize + j);
  51.    return(val);
  52. }
  53.  
  54. void free_data_array(arrptr)
  55. data *arrptr;
  56. {
  57.    /* free the array */
  58.    free((char *)arrptr);
  59. }
  60.